package com.ly.honor.service.impl;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.honor.entity.Clue;
import com.ly.honor.entity.Country;
import com.ly.honor.entity.dto.ClueCreateDTO;
import com.ly.honor.entity.dto.ClueEditDTO;
import com.ly.honor.entity.dto.ClueQueryDTO;
import com.ly.honor.entity.vo.ClueQueryVO;
import com.ly.honor.exception.HonorException;
import com.ly.honor.mapper.CountryMapper;
import com.ly.honor.service.ClueService;
import com.ly.honor.mapper.ClueMapper;
import com.ly.honor.util.HonorUtil;
import com.ly.honor.util.MinioUtil;
import com.ly.honor.util.ResultEnum;
import io.minio.GetPresignedObjectUrlArgs;
import io.minio.MinioClient;
import io.minio.ObjectWriteResponse;
import io.minio.http.Method;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
* @author 17798
* @description 针对表【bs_clue】的数据库操作Service实现
* @createDate 2022-11-16 11:49:49
*/
@Service
public class ClueServiceImpl extends ServiceImpl<ClueMapper, Clue>
    implements ClueService{

    @Autowired
    private ClueMapper clueMapper;

    @Autowired
    private HonorUtil honorUtil;

    @Autowired
    private CountryMapper countryMapper;


    @Override
    public Page<ClueQueryVO> selectCluePage(long pageNum, long pageSize, ClueQueryDTO clueQueryDTO) {
        Page<ClueQueryVO> cluePage = new Page<>(pageNum,pageSize);
        Page<ClueQueryVO> clueQueryVOPage = clueMapper.selectCluePage(cluePage,clueQueryDTO);
        for (ClueQueryVO c:clueQueryVOPage.getRecords()) {
            if(c.getPhoto() != null){
                c.setPhoto(c.getPhoto().split("\\?")[0]);
            }
        }
        for (ClueQueryVO c:clueQueryVOPage.getRecords()) {
            var countryId = c.getCountryId();
            var country = countryMapper.selectById(countryId);
            c.setCountryValue(country.getName());
        }
        return clueQueryVOPage;
    }

    @Override
    public String upload(MultipartFile file) {
        var url = honorUtil.uploadFile(file);
        return url;
    }

    @Override
    public void insertClue(ClueCreateDTO clueDTO) {
        var clue = new Clue();
        if(ObjectUtil.isEmpty(clue)){
            throw new HonorException(ResultEnum.OBJECT_NOT_NULL);
        }
        BeanUtils.copyProperties(clueDTO,clue);
        if(clue.getPhoto() != null && clue.getPhoto().length() != 0){
            clue.setPhoto(clue.getPhoto().split("\\?")[0]);
        }
        clueMapper.insert(clue);
    }

    @Override
    public ClueQueryVO getClueById(long id) {
        var clue = clueMapper.selectById(id);
        var clueQueryVO = new ClueQueryVO();
        BeanUtils.copyProperties(clue,clueQueryVO);
        return clueQueryVO;
    }

    @Override
    public void updateClue(ClueEditDTO clueEditDTO) {
        var clue = new Clue();
        BeanUtils.copyProperties(clueEditDTO,clue);
        if(clue.getPhoto() != null && clue.getPhoto().length() != 0){
            clue.setPhoto(clue.getPhoto().split("\\?")[0]);
        }
        clueMapper.updateById(clue);
    }

    @Override
    public List<ClueQueryVO> getLplClue() {
        List<ClueQueryVO> clueQueryVOList  = new ArrayList<>();
        var country = countryMapper.selectOne(new LambdaQueryWrapper<Country>().eq(Country::getName, "中国"));
        if(ObjectUtil.isEmpty(country)){
            return clueQueryVOList;
        }
        var clueList = clueMapper.selectList(new LambdaQueryWrapper<Clue>().eq(Clue::getCountryId, country.getId()));
        if(clueList == null){
            return clueQueryVOList;
        }
        for(Clue clue : clueList){
            var clueQueryVO = new ClueQueryVO();
            BeanUtils.copyProperties(clue,clueQueryVO);
            clueQueryVO.setCountryValue("中国");
            clueQueryVOList.add(clueQueryVO);
        }
        return clueQueryVOList;
    }

    @Override
    public List<ClueQueryVO> getLckClue() {
        List<ClueQueryVO> clueQueryVOList  = new ArrayList<>();
        var country = countryMapper.selectOne(new LambdaQueryWrapper<Country>().eq(Country::getName, "韩国"));
        if(ObjectUtil.isEmpty(country)){
            return clueQueryVOList;
        }
        var clueList = clueMapper.selectList(new LambdaQueryWrapper<Clue>().eq(Clue::getCountryId, country.getId()));
        if(clueList == null){
            return clueQueryVOList;
        }
        for(Clue clue : clueList){
            var clueQueryVO = new ClueQueryVO();
            BeanUtils.copyProperties(clue,clueQueryVO);
            clueQueryVO.setCountryValue("韩国");
            clueQueryVOList.add(clueQueryVO);
        }
        return clueQueryVOList;
    }

    @Override
    public List<ClueQueryVO> getOtherZoneClue() {
        List<ClueQueryVO> clueQueryVOList  = new ArrayList<>();
        clueQueryVOList = clueMapper.getOtherZoneClue();
        return clueQueryVOList;
    }
}




